package com.atstudy.headline.web.app.controller.users;

import com.atstudy.headline.entity.Contents;
import com.atstudy.headline.entity.Users;
import com.atstudy.headline.enums.BaseStatus;
import com.atstudy.headline.exception.HeadlineException;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.result.ResultCodeEnum;
import com.atstudy.headline.web.app.service.UsersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@RestController
@CrossOrigin
@RequestMapping("/app/users")
public class AppUsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 用户注册
     * @param users 用户注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<Users> register(@RequestBody Users users) {
        // 1. 校验必填字段
        if (users.getUsername() == null || users.getUsername().isEmpty()) {
          throw  new HeadlineException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }
        if (users.getPassword() == null || users.getPassword().isEmpty()) {
           throw  new HeadlineException(ResultCodeEnum.APP_ACCOUNT_ERROR);
        }
        // 2. 检查用户名是否已存在
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUsername, users.getUsername());
        if (usersService.getOne(queryWrapper)!= null) {
           throw  new HeadlineException(ResultCodeEnum.APP_NAME_EXIST_ERROR);
        }

        // 3. 检查邮箱是否已存在（如果提供了邮箱）
        if (users.getEmail() != null && !users.getEmail().isEmpty()) {
            LambdaQueryWrapper<Users> emailQuery = new LambdaQueryWrapper<>();
            emailQuery.eq(Users::getEmail, users.getEmail());
            if (usersService.getOne(emailQuery) != null) {
               throw  new HeadlineException(ResultCodeEnum.APP_EMAIL_EXIST_ERROR);
            }
        }

        // 4. 检查手机号是否已存在（如果提供了手机号）
        if (users.getPhone() != null && !users.getPhone().isEmpty()) {
            LambdaQueryWrapper<Users> phoneQuery = new LambdaQueryWrapper<>();
            phoneQuery.eq(Users::getPhone, users.getPhone());
            if (usersService.getOne(phoneQuery) != null) {
               throw new HeadlineException(ResultCodeEnum.APP_ACCOUNT_EXIST_ERROR);
            }
        }
        // 5. 设置默认值
        users.setRoleCode(1);  // 默认角色为普通用户
        users.setAvatarUrl("default_avatar.jpg");  // 默认头像
        users.setRoleType("user_role");
        users.setGenderType("gender");
        users.setBio("");
        users.setCreateTime(new Date());  // 设置注册时间
        users.setStatus(BaseStatus.ENABLE);  // 默认状态为正常
        // 6. 保存用户
        boolean saved = usersService.save(users);
        if (saved) {
            return Result.ok();
        } else {
            throw new HeadlineException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @GetMapping("/info/{userId}")
    public Result<Users> getUserById(@PathVariable Integer userId){
        return Result.ok(usersService.userInfo(userId));
    }

    /**
     * 修改用户信息
     * @param
     * @return
     * @paramusers
     * @return
     */
    @PostMapping("/updateById")
    public Result updateById(@RequestBody Users users) {
        usersService.updateById(users);
        return Result.ok();
    }
}


